#include <xpp/bas/Thread.h>
#include <xp/bas/stdio.h>
#include <xp/bas/sysapi.h>

#ifdef __linux
#include <mcheck.h>
#endif

using namespace xpp::bas;

class MyThread: public Thread
{
public:
	int run ()
	{
		for (int i = 0; i < 5; i++) {
			xp_sleep (1000);
			xp_printf (XP_TEXT("%d\n"), i);
		}

		return 0;
	}
};

#ifdef __cplusplus
extern "C" {
	int xp_main (int argc, xp_char_t* argv[], xp_char_t** envp);
}
#endif

int xp_main (int argc, xp_char_t* argv[], xp_char_t** envp)
{
#ifdef __linux
	mtrace ();
#endif	
	xp_pid_t pid;

	pid = xp_fork ();
	//pid = fork ();
	if (pid == -1) return -1;

	if (pid == 0) {
		MyThread t;
		if (t.start() == -1) {
			xp_printf (XP_TEXT("failed to start the thread\n"));
			return -1;
		}

		t.join ();
		return 0;
	}

	xp_wait (pid, XP_NULL, 0);
	return 0;
}

